﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace SAIN.MVC.Controllers
{
    public class ModulosController : Controller
    {
        //
        // GET: /Modulos/

        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /Modulos/

        public ActionResult List()
        {
            return View(BC.Configuration.ModulosBC.GetList());
        }
        [HttpPost]
        public ActionResult GetJSON(int iModuleId)
        {
            try
            {
                var a = (from item in BC.Configuration.ModulosBC.GetList()
                         where item.iModuleId == iModuleId
                         select item).Single();
                return Json(a,JsonRequestBehavior.DenyGet);
            }
            catch(Exception ex)
            {
                return Json(ex);
            }
        }
        //
        // GET: /Modulos/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /Modulos/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Modulos/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /Modulos/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /Modulos/Edit/5

        [HttpPost]
        public ActionResult Edit(int iModuleId, string sModuleName, string sDescripcion)
        {
            try
            {
                BE.Configuracion.ModuloBE item = new BE.Configuracion.ModuloBE()
                {
                    iModuleId = iModuleId,
                    sModuleName = sModuleName,
                    sDescription = sDescripcion
                };
                item = BC.Configuration.ModulosBC.Update(item);
 
                return RedirectToAction("List");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /Modulos/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        public ActionResult GetFunctionList(int iModuleId)
        {
            List<BE.Configuracion.FuncionBE> lista = new List<BE.Configuracion.FuncionBE>();

            lista = BC.Configuration.ModulosBC.GetFunctionsByModule(
                new BE.Configuracion.ModuloBE() { iModuleId = iModuleId });

            return View(lista);
        }

        //
        // POST: /Modulos/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
